/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 * Work on normal case
 * @author: jin Chen 
 * @date: Mar 8, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.plot.control.tools;


import edu.psu.geovista.app.pcp.plot.gui.BasicPcpUI;
import edu.psu.geovista.app.pcp.plot.layer.DataLayer;
import edu.psu.geovista.app.pcp.toolkits.collection.CollectionUtils;
import edu.psu.geovista.app.pcp.toolkits.group.DataIDSet;
import edu.psu.geovista.app.plot.layer.Layer;
import edu.psu.geovista.classification.ClassificationResultItf;
import edu.psu.geovista.data.model.AppDataModel;
import edu.psu.geovista.ui.event.FreezeEvent;
import edu.psu.geovista.ui.event.SelectionEvent;

import java.awt.*;
import java.util.HashSet;

public  class NormalDModeActor extends BasicDModeActor{
    public NormalDModeActor(DataSelectTool datatool) {
        super(datatool);
    }


    public void fireIndicationChange(int id) {
         this.getTool().getUI().getPlot().fireIndicationChanged(id);
    }


    public void processIndication(int id, int x, int y) {
        //if pcp in on summary mode, it does not make sense to highlight a summary line upon external source(e.g.Map) indicate a single record

        final DataSelectTool tool = this.getTool();
        if(this.getTool().isHightLightMode()) {
        tool.highLightData(id,x,y);
        }
    }
    public void processFreeze(FreezeEvent e){
         if(e.getMode() ==DataSelectTool.MODE_Normal){ //todo only support normal mode for now , need support more
            final DataSelectTool tool = this.getTool();
             int fact = e.getFreezeAction();
             boolean fireEvent = false;//since process external event, not fire event any more to avoid loop call
             //if(fact==FreezeEvent.ACTION_FREEZE){
                int[] ids = e.getIds();
                HashSet idSet = CollectionUtils.convertArrayToSet(ids);

                tool.freezeData(idSet,fireEvent);
             /*}
             else if(fact==FreezeEvent.ACTION_UNFREEZE_ALL){
                tool.freezeData(new HashSet(0),fireEvent );

             }*/
         }
     }


    /**
     * act on a external selectevent
     *  
     */
    public void processSelection(SelectionEvent e) {
         int[] ids=e.getSelection() ;
         if(ids.length<=0) return;
         this.getTool().upDateAndShowSelection(ids);
    }

    public void processClassification(ClassificationResultItf cr) {
        //System.out.println("");
        // 
    }
    /**
     * - set datalayer color
     * - determine id to display
     */
    public void updateOnSwitchMode() {
        //declare variables
        DataIDSet ids=null;
        final DataSelectTool tool = this.getTool();
        BasicPcpUI ui = tool.getUI();


         DataLayer srLayer=(DataLayer)ui.getLayer(Layer.TopData_LAYER );
         AppDataModel dataModel=(AppDataModel) ui.getPlot().getDataModel() ;



         //common procedure
         super.updateOnSwitchMode();


         tool.updateServiceDataModel(dataModel);
         //vc.doClassification();




         //1. change paint stroke on data layer. So summary mode paint strong lines
         srLayer.setStrongStrokeMode(false) ;

         //update data colors for dataLayer
         DataLayer dataLayer=(DataLayer)ui.getLayer(Layer.BgData_LAYER );

         Color[] recColors=this.getTool().colorFirer.getRecordColor() ;
         if(dataLayer!=null)
         dataLayer.setColors(recColors);//all-data layer
          /**
          *  
          *  
          *  
          *  
          */
         srLayer.setColors(recColors);//showRange dataLayer


        //update ids
        HashSet IDs=new HashSet();
        //Object oids=IDs.toArray() ;
        ids=new  DataIDSet(IDs,DataIDSet.TYPE_CategoryData );
        this.getTool().showDataGroup(ids);//update all layer . ids is empty set


    }
    //usually act on colorArraryEvent
    public void setDataLayerColors(Color[] colors) {
            BasicPcpUI ui = this.getTool().getUI();
            DataLayer srLayer=(DataLayer)ui.getLayer(Layer.TopData_LAYER );
            DataLayer dataLayer=(DataLayer)ui.getLayer(Layer.BgData_LAYER );
            if(srLayer!=null){//=null when reload a different data todo check why
            srLayer.setColors(colors);
            srLayer.repaintLayer() ;
            }
            if(dataLayer!=null){// =null when reload a different data
                dataLayer.setColors(colors);
                dataLayer.repaintLayer() ;
            }
            this.getTool().repaintPlot() ;
    }

    public void fireColorArrayEvent() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    /*public int[] getMemberIds(DataIDGroup grpData,HashSet memberIds) {

        int[] ids=null;
        ids=DataIDSet.convertToIntArray(memberIds );
        return ids;

    }*/
    public int[] getMemberIds(HashSet summaryIds){
        final int[] ids = CollectionUtils.convertToIntArray(summaryIds);
        return ids;
    }


}
